package com.etime.shyauth.controller;

import com.etime.shyauth.config.BaseMessage;
import com.etime.shyauth.config.ListMessage;
import com.etime.shyauth.config.PojoMessage;
import com.etime.shyauth.pojo.SysFunction;
import com.etime.shyauth.services.interfaces.SysFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统功能管理controller接口
 */
@Controller
@RequestMapping("/sys/fun")
@ResponseBody
@Api(tags = "系统功能管理数据接口")
public class SysFunctionController {
    @Autowired
    private SysFunctionService service;

    @ApiOperation("增加系统功能")
    @ApiImplicitParam("funid自增长；子系统的funpid=-1;")
    @PostMapping("/insert")
    public BaseMessage insertSysFunction(SysFunction function){
        int rtn = service.insertSysFunction(function);
        BaseMessage msg = new BaseMessage();
        msg.setCode(rtn);
        return msg;
    }

    @ApiOperation("根据id修改系统功能")
    @ApiImplicitParam("需要所有属性进行赋值，未赋值的属性将被修改为null")
    @PostMapping("/update")
    public BaseMessage updateSysFunction(SysFunction function){
        int rtn = service.updateSysFunction(function);
        BaseMessage msg = new BaseMessage();
        msg.setCode(rtn);
        return msg;
    }

    @ApiOperation("根据id查询一条系统功能的详细信息")
    @ApiImplicitParam("系统功能id，必须为int类型数据")
    @GetMapping("/get/{funid}")
    public PojoMessage<SysFunction> selectSysFunctionById(@PathVariable("funid") int funid){
        SysFunction fun = service.selectSysFunctionById(funid);
        PojoMessage<SysFunction> msg = new PojoMessage<>();
        if(fun!=null){
            msg.setCode(1);
            msg.setData(fun);
        }else{
            msg.setCode(0);
            msg.setMsg("没有指定的系统功能");
        }
        return msg;
    }

    @ApiOperation("根据条件查询系统功能列表")
    @ApiImplicitParam("可用条件：funtype(功能类型）；funstate(功能状态）;funpid(父功能id）" +
            "以上条件为null或者=-1则不使用此条件；funpid=0不使用此条件")
    @GetMapping("/list")
    public ListMessage<SysFunction> selectSysFunctionList(SysFunction function){
        List<SysFunction> list = service.selectSysFunction(function);
        ListMessage<SysFunction> msg = new ListMessage<>();
        if(list!=null){
            msg.setCode(1);
            msg.setDatalist(list);
        }else{
            msg.setCode(0);
            msg.setMsg("没有查询到指定数据");
        }
        return msg;
    }
}
